package com.lixiaoyu.dao;

import com.lixiaoyu.entity.Express;
import com.lixiaoyu.entity.ExpressBox;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 现在是用来存储快递数据的类
 */
public class ExpressDB {
    Express express = new Express();

    ExpressBox expressBox = new ExpressBox();

    //容量50
    //每个里头放4个数据:取件码-快递柜号-快递单号-快递公司
    String[][] EXPRESSBOX = new String[50][4];

    //账号
    String[] userName = new String[2];
    //密码
    int[] passWord = new int[2];
    //存放账号密码
    String[][] userPassWord = new String[2][2];


    //初始化数据
    {
        //取件码
        EXPRESSBOX[0][0] = "212301";
        //快递柜
        EXPRESSBOX[0][1] = "A01";
        //快递单号
        EXPRESSBOX[0][2] = "SF202103230001";
        //快递公司
        EXPRESSBOX[0][3] = "顺丰快递";
        //取件码
        EXPRESSBOX[1][0] = "212302";
        //快递柜
        EXPRESSBOX[1][1] = "A02";
        //快递单号
        EXPRESSBOX[1][2] = "SF202103230002";
        //快递公司
        EXPRESSBOX[1][3] = "顺丰快递";
        userName[0] = "user";
        userName[1] = "admin";
        passWord[0] = 123456;
        passWord[1] = 123456;
        //存放账号密码
        userPassWord[0][0] = userName[0];
        userPassWord[0][1] = Integer.toString(passWord[0]);
        userPassWord[1][0] = userName[1];
        userPassWord[1][1] = Integer.toString(passWord[1]);
    }

    /**
     * 查询快递信息
     * @param getExpressCode
     * @return
     */
    public String[][] getSelectUserExpress(int getExpressCode) {
        String[][] a = new String[1][4];
        for(int i = 0; i < EXPRESSBOX.length; i++){
            if(String.valueOf(getExpressCode).equals(EXPRESSBOX[i][0])){
                a[0][0] = EXPRESSBOX[i][0];
                a[0][1] = EXPRESSBOX[i][1];
                a[0][2] = EXPRESSBOX[i][2];
                a[0][3] = EXPRESSBOX[i][3];
                break;
            }

        }
        return a;
    }

    /**
     * 查询密码
     * @param userName : 账户
     * @return
     */
    public String[][] getUserPassWord(String userName) {
        String[][] up = new String[1][2];
        for(int i = 0; i < userPassWord.length; i++){
            if(userPassWord[i][0].equals(userName)){
                up[0][0] = userPassWord[i][0];
                up[0][1] = userPassWord[i][1];
                break;
            }
        }
        return up;
    }

    /**
     * 删除取件码快递
     * @param getExpressCode
     */
    public void deleteUserExpress(int getExpressCode) {
        for(int i = 0; i < EXPRESSBOX.length; i++){
            if(String.valueOf(getExpressCode).equals(EXPRESSBOX[i][0])){
                EXPRESSBOX[i][0] = null;
                EXPRESSBOX[i][1] = null;
                EXPRESSBOX[i][2] = null;
                EXPRESSBOX[i][3] = null;
                break;
            }
        }
    }

    /**
     * 增加快递
     * @param express
     * @return 返回取件码和柜号
     */
    public ExpressBox addAdminExpress(Express express) {
        LocalDateTime time=LocalDateTime.now();
        String format = time.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        //存放 快递柜和取件码
        expressBox = new ExpressBox();
        //取件码
        int expresscode = 0;
        //快递柜号
        String expressBoxcode = "A00";

        for(int i = 0 ; i < EXPRESSBOX.length; i++){
            if(EXPRESSBOX[i][0] == null){
                if(i+1 < 10){
                    expressBoxcode = "A0" + (i+1);
                }else{
                    expressBoxcode = "A" + (i+1);
                }
                //快递尾号
                String code;
                //这地方可能快递柜是  1 那么就无法生成2为所以要在后面补0
                if(i + 1 < 10){
                    //设计有问题 所以得进行转型
                    code = String.valueOf(i + 1);
                    code = "0" + code;
                }else{
                    code = String.valueOf(i + 1);
                }
                //取件码生成: 2021年3月24号 2124  后两位根据快递柜数量判断 例如有99个  顺序寻找寻找为null的空柜子 例如：22  结果取件码-212422
                expresscode = Integer.parseInt(format.substring(2,4) + format.substring(6,8) + code);
                //存放快递
                EXPRESSBOX[i][0] = String.valueOf(expresscode);
                EXPRESSBOX[i][1] = expressBoxcode;
                EXPRESSBOX[i][2] = express.getExpressCode();
                EXPRESSBOX[i][3] = express.getExpressName();
                break;
            }
        }
        expressBox.setExpressBoxCode(expressBoxcode);
        expressBox.setGetExpressCode(expresscode);
        return expressBox;
    }


    /**
     * 修改快递
     * @param next 取件码
     * @param next1 快递单号
     * @param next2 快递公司
     * @return
     */
    public int updateAdminExpress(int next, String next1, String next2) {
        for(int i = 0; i < EXPRESSBOX.length; i++){
            if(String.valueOf(next).equals(EXPRESSBOX[i][0])){
                EXPRESSBOX[i][2] = next1;
                EXPRESSBOX[i][3] = next2;
                return 1;
            }
        }
        return 0;
    }

    public String[][] selectAdminExpress() {

        return EXPRESSBOX;
    }
}
